package com.dc.service.impl;

import com.dc.dao.AddressDao;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Address;
import com.dc.service.AddressService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressDao addressDao;
    public BaseModel selectPageAddress(String user_name, Integer curr_page, Integer page_size,BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Address> address = addressDao.selectPageAddress(user_name);
        PageInfo pageInfo = new PageInfo(address,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询地址成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel addAddress(Address address, BaseModel baseModel) throws Exception {
        int result = addressDao.insertAddress(address);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.ADD_ADDRESS_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增地址成功");
        return baseModel;
    }

    public BaseModel updateAddress(Address address, BaseModel baseModel) throws Exception {
        int result = addressDao.updateAddress(address);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_ADDRESS_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改地址成功");
        return baseModel;
    }

    public BaseModel deletedAddress(String ids, BaseModel baseModel) throws Exception {
        String[] idArry = ids.split(",");
        int result = addressDao.deletedAddress(idArry);
        if(result <=0 ){
            throw new BusinessException(ErrorMessageEnum.DELETED_ORDER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"条记录成功");
        return baseModel;
    }

    public BaseModel selectAddressById(int id, BaseModel baseModel) throws Exception {
        List<Address> address = addressDao.selectAddressById(id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询成功");
        baseModel.setData(address);
        return baseModel;
    }

    public BaseModel selectAddressByid(int id, BaseModel baseModel) throws Exception {
        Address address = addressDao.selectAddressByid(id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询成功");
        baseModel.setData(address);
        return baseModel;
    }
}
